const app = getApp();
const {
  mobile_login,
  mobileCode,
  getPhoneNumber,
  wxLogin
} = require('../../config');
let timer;
let timer1;
let timer2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 60,
    vipInfo: {},
    login: true,
    isLoginning: false, // 登录中不能重复点击
    timeFlag: false,//验证码 60s
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('login_mobile')
    // let unionid = wx.getStorageSync('unionid')
    // console.log(app.globalData.token, 'login', unionid)

    if (app.globalData.token){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  // 登陆
  formSubmit: function(e) {
    let mobile = e.detail.value.mobile;
    let authCode = e.detail.value.authCode;
    if (!mobile) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (!authCode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    let openid = app.globalData.openid;
    let unionid = wx.getStorageSync('unionid');
    this.isLoginning = true
    wx.request({
      url: mobile_login,
      data: {
        mobile: mobile,
        validate: authCode,
        type: 1,
        openid: openid,
        unionid: unionid,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      method: 'POST',
      success: (res) => {
        this.isLoginning = false
        if (res.data.code == 0) {
          let token = res.data.data.token;
          app.globalData.token = token;
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else if (res.data.code == 104) {
          wx.navigateTo({
            url: `/pages/info_supply/info_supply?type=2&validate=${authCode}&mobile=${mobile}`,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      }
    })
  },
  // 获取验证码
  getCode: function(e) {
    let that = this;
    let {
      timeFlag,
      phone,
    } = this.data;
    let time = 60;
    if (!(/^1\d{10}$/.test(phone))) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      })
      return;
    }
    if (timeFlag) {
      return;
    }
    clearTimeout(timer1);
    timer1 = setTimeout(() => {
      that.setData({
        timeFlag: true,
      }, () => {
        wx.request({
          url: mobileCode,
          data: {
            mobile: phone,
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
          },
          success: (res) => {
            if (res.data.code != 0){
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
              clearInterval(timer);
              that.setData({
                timeFlag: false,
              });
            }
          }
        });
      });
      timer = setInterval(() => {
        time--;
        that.setData({
          time: time,
        });
        if (time <= 0) {
          clearInterval(timer);
          that.setData({
            time: 60,
            timeFlag: false,
          })
        }
      }, 1000)
    }, 500)
  },
  // 手机号输入
  inputPhone: function(e) {
    let phone = e.detail.value
    this.setData({
      phone,
    })
  },

  getPhoneNumber: function(e) {
    let openid = app.globalData.openid;
    console.log('openid=', openid)
    let unionid = wx.getStorageSync('unionid');
    app.appRequest({
      url: getPhoneNumber,
      data: {
        unionid: unionid,
        openid: openid,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
      },
      method: 'POST',
      success: (res) => {
        if (res.code == 0) {
          app.globalData.token = res.data.token;
          if (res.data.userExist) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else {
            let phoneNumber = res.data.phoneNumber;
            wx.navigateTo({
              url: `/pages/info_supply/info_supply?mobile=${phoneNumber}&type=0`,
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
          })
        }
      }
    });
  }
})